/*
 *  This file is part of AndroidIDE.
 *
 *  AndroidIDE is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AndroidIDE is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *   along with AndroidIDE.  If not, see <https://www.gnu.org/licenses/>.
 */

package com.itsaky.androidide.uidesigner

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import com.itsaky.androidide.app.BaseIDEActivity
import com.itsaky.androidide.editor.language.treesitter.XMLLanguage
import com.itsaky.androidide.editor.schemes.IDEColorSchemeProvider
import com.itsaky.androidide.editor.ui.IDEEditor
import com.itsaky.androidide.preferences.internal.fontSize
import com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE
import com.itsaky.androidide.uidesigner.databinding.ActivityShowXmlBinding
import com.itsaky.androidide.utils.ILogger
import com.itsaky.androidide.utils.flashInfo
import com.itsaky.androidide.utils.jetbrainsMono
import io.github.rosemoe.sora.event.Unsubscribe
import io.github.rosemoe.sora.widget.CodeEditor

/**
 * An activity to show the XML code generated by UI Designer.
 *
 * @author Akash Yadav
 */
class ShowXmlActivity : BaseIDEActivity() {

  private var editor: CodeEditor? = null
  private var unsubscribe: Unsubscribe? = null
  private var binding: ActivityShowXmlBinding? = null

  private val log = ILogger.newInstance("ShowXmlActivity")

  companion object {
    const val KEY_XML = "ide.uidesigner.viewXml.Xml"
  }

  override fun bindLayout(): View {
    this.binding = ActivityShowXmlBinding.inflate(layoutInflater)
    this.editor = IDEEditor(this)

    setSupportActionBar(this.binding!!.toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)

    this.binding!!.toolbar.setNavigationOnClickListener { finish() }

    this.editor?.let { editor ->
      editor.colorScheme = SchemeAndroidIDE.newInstance(this@ShowXmlActivity)
      editor.editable = false
      editor.typefaceText = jetbrainsMono()
      editor.typefaceLineNumber = jetbrainsMono()
      editor.setText(intent?.getStringExtra(KEY_XML) ?: "")
      editor.setTextSize(fontSize)

      IDEColorSchemeProvider.readSchemeAsync(
        context = this,
        coroutineScope = activityScope,
        type = XMLLanguage.TS_TYPE
      ) {
        if (it == null) {
          log.error("Unable to load color sheme")
          return@readSchemeAsync
        }

        editor.colorScheme = it
        editor.setEditorLanguage(XMLLanguage(this))
      }

      binding!!.editorContainer.let { container ->
        container.removeAllViews()
        container.addView(editor, ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT))
      }
    }

    this.binding?.copy?.setOnClickListener {
      this.editor?.copyText(false)
      flashInfo(R.string.copied)
    }

    return this.binding!!.root
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
  }

  override fun onDestroy() {
    super.onDestroy()
    this.binding = null
    this.unsubscribe?.unsubscribe()
    this.editor?.release()
    this.editor = null
  }
}
